﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace uQR.XsltExtensions
{
    public class uQRExtensions
    {
        public static string GetQRCodeUrl(string data)
        {
            return string.Format("{0}?data={1}",
                GetHandlerUrl(),
                HttpUtility.UrlEncode(data));
        }

        public static string GetQRCodeUrl(string data, int width, int height)
        {
            return string.Format("{0}?data={1}&width={2}&height={3}",
                GetHandlerUrl(),
                HttpUtility.UrlEncode(data),
                width,
                height);
        }

        public static string GetQRCodeUrl(string data, int width, int height, string errorCorrectionLevel)
        {
            return string.Format("{0}?data={1}&width={2}&height={3}&correction={4}",
                GetHandlerUrl(),
                HttpUtility.UrlEncode(data),
                width,
                height,
                errorCorrectionLevel);
        }

        private static string GetHandlerUrl()
        {
            var appPath = "";

            if (HttpContext.Current != null)
                appPath = HttpContext.Current.Request.ApplicationPath.TrimEnd('/');

            return appPath + "/umbraco/plugins/uqr/uqrhandler.ashx";
        }
    }
}